

package com.bei_en.teacher.admin.service.questions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.questions.bean.QuestionsType;
import com.bei_en.teacher.admin.model.questions.bean.QuestionsType2;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsTypeVo;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsTypeCondition;
import com.bei_en.teacher.admin.persistence.dao.questions.QuestionsTypeDao;

/**
 * 
 * @author Guocg
 * @date 2016-8-18 9:18:37
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class QuestionsTypeService extends BaseService{

    @Autowired
    private QuestionsTypeDao questionsTypeDao;

    public void insert(QuestionsTypeVo model) {

    	 questionsTypeDao.insert(model.getQuestionsType());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(QuestionsTypeVo model) {
//
//	questionsTypeDao.insert(model.getQuestionsType());
//	return questionsTypeDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

    	questionsTypeDao.delete(modelInteger);
    }

    public QuestionsTypeVo findById(Integer modelInteger) {

    	QuestionsTypeVo model = new QuestionsTypeVo(questionsTypeDao.findById(modelInteger));
    	return model;
    }

    public void update(QuestionsTypeVo model) {

    	questionsTypeDao.update(model.getQuestionsType());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(QuestionsTypeVo model) {

    	questionsTypeDao.updateSelective(model.getQuestionsType());
    }

    public int countAll() {

    	return questionsTypeDao.countAll();
    }

    public List<QuestionsTypeVo> findAll() {

		List<QuestionsTypeVo> list = new ArrayList<QuestionsTypeVo>();
		List<QuestionsType> list1 = questionsTypeDao.findAll();
		for (QuestionsType model : list1) {
		    list.add(new QuestionsTypeVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return questionsTypeDao.findAllIds();
    }
    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public JSONArray  findIntegralLogsMapPage(QuestionsTypeCondition vo) {

		Integer totalCount = searchQuestionsTypeNum(vo);
		vo.setTotalCount(totalCount);
		JSONArray array=new JSONArray();
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<QuestionsType>     list = questionsTypeDao.findPageBreakByCondition(vo, rb);
		    QuestionsType integralLogsVo=null;
		    JSONObject object;
			   for (int i = 0; i < list.size(); i++) {
				   object=new JSONObject();
				   integralLogsVo=list.get(i);
				   object.accumulate("value", integralLogsVo.getName().trim());
				   object.accumulate("lable", integralLogsVo.getId()+"");
				   array.add(object);
			}
		}

		return array;
    }
    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<QuestionsTypeVo> findQuestionsTypePage(QuestionsTypeCondition vo) {

		Integer totalCount = searchQuestionsTypeNum(vo);
		List<QuestionsTypeVo> list = new ArrayList<QuestionsTypeVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "weight  desc,  id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<QuestionsType> list1 = questionsTypeDao.findPageBreakByCondition(vo, rb);
		    for (QuestionsType model : list1) {
		    	list.add(new QuestionsTypeVo(model));
		    }
	
		}

		return list;
    }
    

    private Integer searchQuestionsTypeNum(QuestionsTypeCondition Vo) {

    	return questionsTypeDao.findNumberByCondition(Vo);
    }
    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<QuestionsType2> findQuestionsTypePage2(QuestionsTypeCondition vo) {

		Integer totalCount = searchQuestionsTypeNum(vo);
		vo.setTotalCount(totalCount);
	    List<QuestionsType2> list1 =new  ArrayList<QuestionsType2>();
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "spot_id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		   list1 = questionsTypeDao.findPageBreakByCondition2(vo, rb);
		}

		return list1;
    }

    private Integer searchQuestionsTypeNum2(QuestionsTypeCondition Vo) {

    	return questionsTypeDao.findNumberByCondition2(Vo);
    }
}
